package com.example.xViewChat.UI;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.example.xViewChat.NettyClient.PushManager;
import com.example.xViewChat.R;
import com.example.xViewChat.UI.Fragment.FriendListFragment;
import com.example.xViewChat.UI.Fragment.MessageFragment;
import com.example.xViewChat.UI.Fragment.NearByFragment;
import com.example.xViewChat.UI.Fragment.UserInfoFragment;

/**
 * Created by hasee on 2016-04-13.
 */
public class MainActivity extends EventListenerActivity {

    protected static final String TAG = "MainActivity";
    private Context mContext;
    private ImageButton mNews,mConstact,mDeynaimic,mSetting;
    private View mPopView;
    private View currentButton;

    private TextView app_cancle;
    private TextView app_exit;
    private TextView app_change;

    private PopupWindow mPopupWindow;
    private LinearLayout buttomBarGroup;


    @Override
    protected void initViews() {
        super.initViews();

        buttomBarGroup=(LinearLayout) findViewById(R.id.buttom_bar_group);
        mNews=(ImageButton) findViewById(R.id.buttom_news);
        mConstact=(ImageButton) findViewById(R.id.buttom_constact);
        mDeynaimic=(ImageButton) findViewById(R.id.buttom_deynaimic);
        mSetting=(ImageButton) findViewById(R.id.buttom_setting);

        mNews.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                MessageFragment messageFragment=new MessageFragment();
                ft.replace(R.id.fl_content, messageFragment,MainActivity.TAG);
                ft.commit();
                setButton(v);
            }
        });
        mConstact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                FriendListFragment friendListFragment=new FriendListFragment();
                ft.replace(R.id.fl_content, friendListFragment,MainActivity.TAG);
                ft.commit();
                setButton(v);
            }
        });
        mDeynaimic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                NearByFragment nearByFragment=new NearByFragment();
                ft.replace(R.id.fl_content, nearByFragment,MainActivity.TAG);
                ft.commit();
                setButton(v);
            }
        });
        mSetting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm=getSupportFragmentManager();
                FragmentTransaction ft=fm.beginTransaction();
                UserInfoFragment userInfoFragment=new UserInfoFragment();
                ft.replace(R.id.fl_content, userInfoFragment,MainActivity.TAG);
                ft.commit();
                setButton(v);
            }
        });

    }

    private void setButton(View v){
        if(currentButton!=null&&currentButton.getId()!=v.getId()){
            currentButton.setEnabled(true);
        }
        v.setEnabled(false);
        currentButton=v;
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainactivity);

        initViews();
    }

    @Override
    public void onBackPressed() {
        finish();
        PushManager.destory(this);
    }
}
